home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / geom_lib / poly_cln.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-30  |  2.8 KB  |  95 lines

  1. /******************************************************************************
  2. * Poly_cln.c - Clean polygonal data.                          *
  3. *******************************************************************************
  4. * Written by Gershon Elber, June 1993.                          *
  5. ******************************************************************************/
  6.  
  7. #include "irit_sm.h"
  8. #include "allocate.h"
  9. #include "iritprsr.h"
  10. #include "poly_cln.h"
  11.  
  12. /*****************************************************************************
  13. *   Routine to clean up polygons - delete zero length edges, and polygons    *
  14. * with less than 3 vertices.                             *
  15. *****************************************************************************/
  16. void CleanUpPolygonList(IPPolygonStruct **PPolygons)
  17. {
  18.     IPPolygonStruct *PPHead, *PPLast;
  19.     IPVertexStruct *PVHead, *PVTemp, *PVNext;
  20.  
  21.     PPLast = PPHead = *PPolygons;
  22.  
  23.     while (PPHead != NULL) {
  24.     PVHead = PVTemp = PPHead -> PVertex;
  25.     /* Look for zero length edges (V == V -> Pnext): */
  26.     do {
  27.         PVNext = PVTemp -> Pnext;
  28.         if (PT_APX_EQ(PVTemp -> Coord, PVNext -> Coord)) { /* Del PVNext.*/
  29.         PVTemp -> Pnext = PVTemp -> Pnext -> Pnext;
  30.         PVNext -> Pnext = NULL;            /* Free only PVNext. */
  31.         if (PVHead == PVNext) {          /* If we actually kill header. */
  32.             PPHead -> PVertex = PVHead = PVTemp;
  33.             break;
  34.         }
  35.         IPFreeVertexList(PVNext);
  36.         }
  37.         else
  38.         PVTemp = PVTemp -> Pnext;
  39.     }
  40.     while (PVTemp != NULL && PVTemp != PVHead);
  41.  
  42.     /* Now test if at list 3 vertices in polygon, otherwise delete it:   */
  43.     if (PVHead == PVHead -> Pnext ||         /* One vertex only. */
  44.         PVHead == PVHead -> Pnext -> Pnext) {      /* Two vertices only. */
  45.         if (PPHead == *PPolygons) {
  46.         *PPolygons = (*PPolygons) -> Pnext;
  47.         IPFreePolygon(PPHead);
  48.         PPHead = (*PPolygons);
  49.         }
  50.         else {
  51.         PPLast -> Pnext = PPHead -> Pnext;
  52.         IPFreePolygon(PPHead);
  53.         PPHead = PPLast -> Pnext;
  54.         }
  55.     }
  56.     else {
  57.         PPLast = PPHead;
  58.         PPHead = PPHead -> Pnext;
  59.     }
  60.     }
  61. }
  62.  
  63. /*****************************************************************************
  64. *   Routine to clean up polylines of zero length.                 *
  65. *****************************************************************************/
  66. void CleanUpPolylineList(IPPolygonStruct **PPolylines)
  67. {
  68.     IPPolygonStruct
  69.     *Poly = *PPolylines;
  70.  
  71.     /* Remove empty sized polylines. */
  72.     while (Poly != NULL &&
  73.        (Poly -> PVertex == NULL ||
  74.         Poly -> PVertex -> Pnext == NULL)) {
  75.     *PPolylines = (*PPolylines) -> Pnext;
  76.     IPFreePolygon(Poly);
  77.     Poly = *PPolylines;
  78.     }
  79.  
  80.     if (Poly && Poly -> Pnext) {
  81.     while (Poly -> Pnext != NULL) {
  82.         if (Poly -> Pnext -> PVertex == NULL ||
  83.         Poly -> Pnext -> PVertex -> Pnext == NULL) {
  84.         IPPolygonStruct
  85.             *TmpPoly = Poly -> Pnext;
  86.  
  87.         Poly -> Pnext = TmpPoly -> Pnext;
  88.         IPFreePolygon(TmpPoly);
  89.         }
  90.         else
  91.         Poly = Poly ->Pnext;
  92.     }
  93.     }
  94. }
  95.